import { getShowString, LineHeight } from "../utils/mirUtil";
import { MoneyEnum } from "../配置/基础配置";

const itemList = [
    {
        name: "金创药(小量)",
        num: 10,
        priceType: MoneyEnum.Gold,
        price: 100,
        hint: "单价100"
    },
    {
        name: "魔法药(小量)",
        num: 10,
        priceType: MoneyEnum.Gold,
        price: 100,
        hint: "单价100"
    },
    {
        name: "金创药(中量)",
        num: 10,
        priceType: MoneyEnum.Gold,
        price: 300,
        hint: "单价300"
    },
    {
        name: "魔法药(中量)",
        num: 10,
        priceType: MoneyEnum.Gold,
        price: 300,
        hint: "单价300"
    },
    {
        name: "强效金创药",
        num: 10,
        priceType: MoneyEnum.Gold,
        price: 900,
    },
    {
        name: "强效魔法药",
        num: 10,
        priceType: MoneyEnum.Gold,
        price: 900,
        hint: "单价900"
    },
    {
        name: "强效太阳水",
        num: 10,
        priceType: MoneyEnum.Gold,
        price: 15000,
        hint: "单价1.5W",
    },
    {
        name: "万年雪霜",
        num: 10,
        priceType: MoneyEnum.Gold,
        price: 40000,
        hint: "单价4W",
    },
    {
        name: "疗伤药",
        num: 10,
        priceType: MoneyEnum.Gold,
        price: 100000,
        hint: "单价10W",
    },
    {
        name: "超级护身符",
        num: 1,
        priceType: MoneyEnum.Gold,
        price: 1,
    },
    {
        name: "随机传送石",
        num: 1,
        priceType: MoneyEnum.Gold,
        price: 1,
    },
    {
        name: "盟重传送石",
        num: 1,
        priceType: MoneyEnum.Gold,
        price: 1,
    },
    {
        name: "修复神水",
        num: 1,
        priceType: MoneyEnum.Gold,
        price: 1,
    },
]

const PageNum = 15;

export function Main(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let showNameList = []
    let page: number = 0;
    if (Args) {
        page = Args.Int[0] || 0
    }

    let maxPage = Math.ceil(itemList.length / PageNum)
    if (page < 0) {
        page = 0;
    }
    if (page > maxPage) {
        page = maxPage
    }
    let showPage = false;
    if (maxPage > 1) {
        showPage = true;
    }

    let start = page * PageNum;
    let end = Math.min(start + PageNum, itemList.length);

    for (let i = start; i < end; i++) {
        let item = itemList[i]
        let fucName
        let name
        if (item.num <= 1) {
            fucName = `商城.购买物品(${i})`
            name = item.name;
        }
        else {
            fucName = `@商城.InputInteger1(请输入物品数量最大值999,${i})`
            name = `${item.name}`
        }

        showNameList.push({
            name: name,
            fucName: fucName,
            hint: item.hint,
            showImg: true
        })
    }

    let ItemsString = getShowString(showNameList, 0, 0, 120, 30, 3);
    if (showPage) {
        ItemsString += `<{S=下一页;C=251;X=220;Y=120;}/@商城.Main(${page - 1})>`
        ItemsString += `<{S=第${page}页;C=255;X=280;Y=120;}>`
        ItemsString += `<{S=上一页;C=251;X=320;Y=120;}/@商城.Main(${page + 1})>`
    }
    Npc.Say(Player, ItemsString)
}


export function InputInteger1(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let num: number = Args.Int[1];
    let index: number = Args.Int[0];
    num = Math.min(num, 999)
    购买物品执行(Player, index, num)
}

export function 购买物品(Npc: TNormNpc, Player: TPlayObject, Args: TArgs) {
    let index = Args.Int[0]
    let item = itemList[index];
    let num = item.num || 1
    购买物品执行(Player, index, num)
}

export function 购买物品执行(Player: TPlayObject, index: number, num: number): void {
    let item = itemList[index];
    let name = item.name
    let moneyType = item.priceType
    let price = item.price
    let totalPrice = price * num
    if (Player.CanTakeGameMoney(moneyType, totalPrice)) {
        Player.TakeGameMoney(moneyType, totalPrice, "商城")
        Player.Give(name, num)
    }
    else {
        Player.MessageBox("你没有足够的货币")
    }
}